<?php
    // +----------------------------------------------------------------------
    // | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ] 
    // +----------------------------------------------------------------------
    // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
    // +----------------------------------------------------------------------
    // | SiteUrl: http://www.luckyhhy.cn
    // +----------------------------------------------------------------------
    // | Author: LuckyHhy <jackhhy520@qq.com>
    // +----------------------------------------------------------------------
    // | Date: 2020/3/6-11:32
    // +----------------------------------------------------------------------
    // | Description:  
    // +----------------------------------------------------------------------


    namespace app\wechat\controller;
    use app\wechat\service\WechatService;
    use library\File;

    class Config extends ChatBase
    {


        /**
         * @throws \think\Exception
         * @throws \think\exception\PDOException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/6
         * @name: index
         * @describe:微信授权绑定
         */
        public function index(){

            $this->thrNotify = url('wechat/push/index', '', false, true);

            /**
             * 提交修改数据
             */
            if ($this->request->isPost()){

                $data=$this->request->post();
                foreach ($data as $k => $v) configGet($k, $v);

                if ($data['wechat_type'] === 'thr') {
                    WechatService::wechat()->setApiNotifyUri($this->thrNotify);
                }
                $this->addLog('微信管理', '修改微信授权配置成功');

                $this->success('微信参数修改成功！');
            }

            $this->title = '微信授权绑定';
            if (!($this->geoip = cache('mygeoip'))) {

                cache('mygeoip', $this->geoip = gethostbyname($this->request->host()), 360);
            }
            $code = encode(url('@admin', '', true, true) . '#' . $this->request->url());

            $this->authurl = config('wechat.service_url') . "/service/api.push/auth/{$code}";

            if (input('?appid') && input('?appkey')) {
                configGet('wechat_type', 'thr');
                configGet('wechat_thr_appid', input('appid'));
                configGet('wechat_thr_appkey', input('appkey'));

                WechatService::wechat()->setApiNotifyUri($this->thrNotify);
            }
            try {
                $this->wechat = WechatService::wechat()->getConfig();

            } catch (\Exception $e) {
                $this->wechat = [];
            }
            return $this->fetch();

        }



        /**
         * @throws \OSS\Core\OssException
         * @throws \think\Exception
         * @throws \think\exception\PDOException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/6
         * @name: pay
         * @describe:微信支付配置
         */
        public function pay(){
            if ($this->request->isPost()) {
                $data=$this->request->post();
                if ($data['wechat_mch_ssl_type'] === 'p12') {
                    if (!($sslp12 = $data['wechat_mch_ssl_p12'])) {
                        $mchid = $data['wechat_mch_id'];

                        $content = File::instance('local')->get($sslp12, true);

                        if (!openssl_pkcs12_read($content, $certs, $mchid)) {

                            $this->error('商户MCH_ID与支付P12证书不匹配！');
                        }
                    }
                }
                foreach ($this->request->post() as $k => $v) configGet($k, $v);

                $this->addLog('微信管理', '修改微信支付配置成功');
                
                $this->success('微信支付配置成功！');
            }

            $this->title = '微信支付配置';
            $file = File::instance('local');
            $this->wechat_mch_ssl_cer = configGet('wechat_mch_ssl_cer');
            $this->wechat_mch_ssl_key = configGet('wechat_mch_ssl_key');
            $this->wechat_mch_ssl_p12 = configGet('wechat_mch_ssl_p12');
            if (!$file->has($this->wechat_mch_ssl_cer, true)) $this->wechat_mch_ssl_cer = '';
            if (!$file->has($this->wechat_mch_ssl_key, true)) $this->wechat_mch_ssl_key = '';
            if (!$file->has($this->wechat_mch_ssl_p12, true)) $this->wechat_mch_ssl_p12 = '';

            return $this->fetch();
        }

    }